#include "stdafx.h"
#include "MouseInput.h"
#include <cassert>

#pragma once

#ifdef _DEBUG

#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
#define new DEBUG_NEW

#endif

namespace banknamespace
{

	Mouse::Mouse(HWND hWnd)
		: m_hWnd(hWnd)
	{
		{
			assert(hWnd != NULL);

			for (ButtonId button = 0; button < MAX_BUTTONS; ++button)
			{
				m_buttonStates[button] = false; // Initialize to up
				m_lastButtonStates[button] = false; // Initialize to up
			}
			m_dblClick = false;
		}
	}

	unsigned int Mouse::getNumButtons() const
	{
		return MAX_BUTTONS;
	}
	vec2 Mouse::getPosition() const
	{
		POINT mousePos;
		GetCursorPos( &mousePos );
		ScreenToClient( m_hWnd, &mousePos);
		return vec2( (float)mousePos.x, (float)mousePos.y );

		//vec2 TODO;
		//TODO.x = TODO.y = 0;
		//return TODO;
	}
	
	bool Mouse::didGoUp(ButtonId button) const
	{
		assert(button < getNumButtons());
		return !m_buttonStates[ button ] && m_lastButtonStates[ button ];
	}
	bool Mouse::didGoDown(ButtonId button) const
	{
		assert(button < getNumButtons());
		return m_buttonStates[ button ] && !m_lastButtonStates[ button ];
	}
	bool Mouse::isDown(ButtonId button) const
	{
		assert(button < getNumButtons());
		return m_buttonStates[button];
	}
	bool Mouse::isDblClick() const
	{
		//assert(button < getNumButtons());
		return m_dblClick;
	}

	void Mouse::update()
	{
		for (ButtonId button = 0; button < MAX_BUTTONS; ++button)
		{
			m_lastButtonStates[button] = m_buttonStates[button]; // Initialize to up
		}
		m_dblClick = false;
	}

	void Mouse::processMessages(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
	{
		switch (message)
		{
		case WM_LBUTTONDOWN:
			m_buttonStates[0] = true;
			break;
		case WM_LBUTTONUP:
			m_buttonStates[0] = false;
			break;
		case WM_RBUTTONDOWN:
			m_buttonStates[1] = true;
			break;
		case WM_RBUTTONUP:
			m_buttonStates[1] = false;
			break;
		case WM_MBUTTONDOWN:
			m_buttonStates[2] = true;
			break;
		case WM_MBUTTONUP:
			m_buttonStates[2] = false;
			break;
		case WM_LBUTTONDBLCLK:
			m_dblClick = true;
			break;
		}
	}

	Mouse& GetTheMouse(HWND hWnd)
	{
		static Mouse mouse(hWnd);
		return mouse;
	}
}

//namespace UseCase
//{
//	class Player : public Sprite, public Mouse::MouseListener
//	{
//	public:
//		virtual void updateSimulation()
//		{
//			Mouse& mouse = ???;
//			if (mouse.didClick())
//			{
//				Explode();
//			}
//		}
//		virtual bool onUp()
//		{
//			FireBullet();
//		}
//		virtual bool onDown() = 0;
//		virtual bool onClick() = 0;
//	}
//}
//
//#endif
